package com.jinkaimen.kqb.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.application.HTApplication;
import com.ht.baselibs.bean.BaseGetBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.igexin.sdk.PushManager;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.AccountBean;
import com.jinkaimen.kqb.constant.Constants;
import com.jinkaimen.kqb.dao.AccountDao;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.service.GetuiService;
import com.jinkaimen.kqb.service.UploadClientIdSevice;
import com.jinkaimen.kqb.thread.ValidateThread;
import com.jinkaimen.kqb.ui.activity.addmerchant.RegisterActivity;
import com.jinkaimen.kqb.utils.DateUtil;
import com.jinkaimen.kqb.utils.ValidateUtils;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {
    private EditText mValidateEditText;
    private EditText mTelView;
    private TextView mLoginBtn, mGetValidateBtn, mTvGoRegister;
    private AccountBean loginReturnBean;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setLeftBtnIcon(R.mipmap.back_black)//
                .setTitleText("登录")//
                .setTitleTextColor(R.color.black)
                .setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_login2;
    }

    @Override
    protected void initChildView() {
        mTelView = (EditText) findViewById(R.id.et_activity_login_tel);
        mValidateEditText = (EditText) findViewById(R.id.et_activity_login_validate);
        mLoginBtn = (TextView) findViewById(R.id.btn_activity_login);
        mTvGoRegister = (TextView) findViewById(R.id.tv_go_register);
        mGetValidateBtn = (TextView) findViewById(R.id.tv_activity_login_get_validate);

    }

    @Override
    protected void initChildData() {
        if (HTApplication.IS_DEBUG_MODE) {
            mTelView.setText("18813076400");
            // mPwdEditText.setText("123456");
        }
//        mTelView.setText(SharedPreferencesUtils.getString("name", ""));
//        mPwdEditText.setText(SharedPreferencesUtils.getString("pass_word", ""));
    }

    @Override
    protected void setChildViewListener() {
        mLoginBtn.setOnClickListener(this);
        mGetValidateBtn.setOnClickListener(this);
        mTvGoRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        final String tel = mTelView.getText().toString();
        String validate = mValidateEditText.getText().toString();
        switch (v.getId()) {
            case R.id.tv_activity_login_register:
                ActivityUtils.startActivity(this, RegisterActivity.class);
                break;
            case R.id.tv_activity_login_get_validate:
                if (!ValidateUtils.isTelValidate(tel)) return;
                showLoadingDialogCanotCancle();
                ApiManager.getApi().sendValidate(
                        "HSS001005",
                        tel, Constants.OEM_NUM,
                        "2"
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        mGetValidateBtn.setEnabled(false);
                        new ValidateThread(mHandler).start();
                        mGetValidateBtn.setTextColor(Color.parseColor("#bebebe"));
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case R.id.btn_activity_login:
                String str = SharedPreferencesUtils.getString("access_Token", "");
                Log.e("----------", str);
                if (TextUtils.isEmpty(tel)) {
                    UIUtils.showToast("请输入手机号");
                    return;
                }

                if (TextUtils.isEmpty(validate)) {
                    return;
                }

                showLoadingDialog();
                ApiManager.getApi().login(
                        "HSS001007",
                        tel,
                        validate, Constants.OEM_NUM
                ).enqueue(new NetCallBack<AccountBean>() {
                    @Override
                    protected void onSuccess(Call<AccountBean> call, Response<AccountBean> response) {
                        Intent downloadIntent = new Intent(LoginActivity.this, UploadClientIdSevice.class);
                        LoginActivity.this.startService(downloadIntent);
                        Intent downloadIntent1 = new Intent(LoginActivity.this, GetuiService.class);
                        LoginActivity.this.startService(downloadIntent1);

                        PushManager.getInstance().initialize(LoginActivity.this, GetuiService.class); // 重新初始化sdk
                        loginReturnBean = response.body();
                        EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_LOGIN));
                        AccountDao.getInstance().addAccount(loginReturnBean);
                        showLoadingDialogCanotCancle();
                        String cid = PushManager.getInstance().getClientid(LoginActivity.this);
                        if (!TextUtils.isEmpty(cid)) {
                            ApiManager.getApi().clientid(
                                    "HSS001002",
                                    cid
                            ).enqueue(new NetCallBack<BaseGetBean>() {
                                @Override
                                protected void onSuccess(Call<BaseGetBean> call, Response<BaseGetBean> response) {
//                                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_BALANCE_CHANGE));
//                                    ActivityUtils.finishActivity(LoginActivity.this);
                                }

                                @Override
                                protected void hiddenProgress() {
                                    hideLodingDialog();
                                }

                            });
                        }

                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case R.id.tv_activity_login_forget_pwd:
                ActivityUtils.startActivity(this, ResetPwdActivity.class);
                break;
            case R.id.tv_go_register:
                ActivityUtils.startActivity(this, RegisterActivity.class);
                break;
        }
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    public void onEvent(EventCenter eventCenter) {
        switch (eventCenter.eventCode) {
            case EventCode.EVENT_CODE_START_LOGIN:
                ActivityUtils.finishActivity(this);
                break;
        }
    }


    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what) {
            case 100:
                mGetValidateBtn.setText(msg.obj.toString());
                break;
            case 200:
                mGetValidateBtn.setTextColor(Color.parseColor("#6284dd"));
                mGetValidateBtn.setText("获取验证码");
                mGetValidateBtn.setEnabled(true);
                break;
//            case 300:
//                mResetPwdVoiceSendCallTV.setText(msg.obj.toString());
//                break;
//            case 400:
//                mResetPwdVoiceSendCallTV.setTextColor(Color.parseColor("#1a9a48"));
//                mResetPwdVoiceSendCallTV.setText("使用语音验证码");
//                mResetPwdVoiceSendCallTV.setEnabled(true);
//                break;
        }
    }

}
